package com.string.actortv.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import com.bumptech.glide.Glide;
import com.string.actortv.R;
import com.string.actortv.enums.PlayerType;
import com.string.actortv.util.*;

public class HistoryActivity extends Activity {

    private Context mContext;
    private SimpleCursorAdapter gridAdapter;
    private GridView gridView;
    private View selectedView;
    private SQLiteHelper sqLiteHelper;
    private Cursor cursor = null;
    private AlertDialog.Builder mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_season_list);
        mContext = this;
        sqLiteHelper = new SQLiteHelper(mContext);
        gridView = this.findViewById(R.id.item_grid);

    }

    private void newInit() {
        if (gridView == null) {
            throw new IllegalArgumentException("the gridView is null");
        }

        //加载数据
        cursor = sqLiteHelper.selectHistory();

        this.gridAdapter = new SimpleCursorAdapter(this,
                R.layout.function_block, cursor, new String[]{"title", "img_url"},
                new int[]{R.id.Item_text, R.id.Item_bt});
        gridAdapter.setViewBinder((view, data, textRepresentation) -> {
            //判断是否为我们要处理的对象
            if (view instanceof ImageView) {
                ImageView iv = (ImageView) view;
                Glide.with(this).load(data.getString(2)).error(R.drawable.no_pic).into(iv);
                return true;
            } else {
                return false;
            }
        });

        this.gridView.setAdapter(gridAdapter);
        this.gridAdapter.notifyDataSetChanged();

        this.gridView.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                }
                View first = gridView.getChildAt(0);
                if (first != null) {
                    first.setBackgroundColor(Color.GRAY);
                    selectedView = first;
                }
            } else {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                    selectedView = null;
                }
            }
        });

        this.gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                }
                if (view != null) {
                    view.setBackgroundColor(Color.GRAY);
                    selectedView = view;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                if (selectedView != null) {
                    selectedView.setBackgroundColor(Color.WHITE);
                    selectedView = null;
                }
            }

        });

        this.gridView.setOnItemLongClickListener((parent, view, position, id) -> {
            cursor.moveToPosition(position);

            mDialog = new AlertDialog.Builder(mContext);
            mDialog.setTitle("删除历史记录");
            mDialog.setMessage("是否删除 " + cursor.getString(1) + " 的播放记录");

            mDialog.setPositiveButton("确定", (dialog, which) -> {
                sqLiteHelper.deleteHistoryByUrl(cursor.getString(3));
                //刷新当前表单
                newInit();
            }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).setCancelable(false).create().show();

            return true;
        });

        this.gridView.setOnItemClickListener((gridView, view, position, id) -> {
            UIHelper.showDialogForLoading(mContext, "正在加载...");
            cursor.moveToPosition(position);
            Log.d("hisId", "" + cursor.getLong(0));
            Log.d("itemTimes", "" + cursor.getLong(5));
            sqLiteHelper.deleteHistoryByUrl(cursor.getString(3));
            sqLiteHelper.insertHistory(cursor.getString(1), cursor.getString(2),
                    cursor.getString(3), cursor.getString(4),
                    cursor.getLong(5), cursor.getInt(6));
            Intent intent1;
            if (cursor.getInt(6) == PlayerType.REN_REN.getCode()) {
                intent1 = new Intent(HistoryActivity.this, AliPlayerActivity.class);
                //重新记录历史记录
                RenrenUtil.getMovieUrlAsync(cursor.getString(3), playUrl -> {
                    intent1.putExtra("url", cursor.getString(3));
                    intent1.putExtra("playUrl", playUrl);
                    intent1.putExtra("hisTimes", cursor.getLong(5));
                    UIHelper.dismiss();
                    startActivity(intent1);
                });
            } else if (cursor.getInt(6) == PlayerType.BDE4.getCode()) {
                intent1 = new Intent(HistoryActivity.this, AliPlayerActivity.class);
                Bde4Util.getMovieUrlAsync(cursor.getString(3), (playUrl, cookie) -> {
                    intent1.putExtra("url", cursor.getString(3));
                    intent1.putExtra("cookie", cookie);
                    intent1.putExtra("playUrl", playUrl);
                    intent1.putExtra("hisTimes", cursor.getLong(5));
                    UIHelper.dismiss();
                    startActivity(intent1);
                });
            } else if (cursor.getInt(6) == PlayerType.IDY_BEE.getCode()) {
                intent1 = new Intent(HistoryActivity.this, AliPlayerActivity.class);
                IdyBeeUtil.getMovieUrlAsync(cursor.getString(3), playUrl -> {
                    intent1.putExtra("url", cursor.getString(3));
                    intent1.putExtra("playUrl", playUrl);
                    intent1.putExtra("hisTimes", cursor.getLong(5));
                    UIHelper.dismiss();
                    startActivity(intent1);
                });
            } else if (cursor.getInt(6) == PlayerType.JXTCD.getCode()) {
                intent1 = new Intent(HistoryActivity.this, AliPlayerActivity.class);
                JxtcdUtil.getMovieUrlAsync(cursor.getString(3), playUrl -> {
                    intent1.putExtra("url", cursor.getString(3));
                    intent1.putExtra("playUrl", playUrl);
                    intent1.putExtra("hisTimes", cursor.getLong(5));
                    UIHelper.dismiss();
                    startActivity(intent1);
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        newInit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

}
